﻿using System;
using System.Collections.Generic;
using System.Linq;
using Framework.Exceptions;
using Framework.Exceptions.RepositoryExceptions;
using Framework.Logging;
using Interfaces.Repositories;
using Interfaces.RepositoryModels.InternalModels.UserRoleModels;
using Microsoft.Practices.EnterpriseLibrary.Logging;


namespace SQLRepositories.UserRoleRepositories
{
    public class UserRepository : EntityBaseRepository<UserModel>, IUserRepository
    {
        
        public UserRepository(IDBEconomicInformationServiceContext context):base(context)
        {}

        public void Create(UserModel item)
        {
            if(item == null) 
                  throw new ArgumentNullException("item");
            try
            {
                Context.Users.Add(item);
                Context.SaveChanges();

                Logger.Write(string.Format("User with id:{0} successfully saved", item.Id), 
                             LogCategory.DAS, (int)EventPriority.Normal);
            }
            catch (Exception e)
            {
                if (ExceptionHandlingManager.HandleException(e, PolicyNames.DASPolicy))
                    throw;
            }
           
        }

        public UserModel GetById(Guid id)
        {
            if (id.Equals(Guid.Empty))
                throw  ExceptionHelper.CreateEmptyGuidException("id");

            UserModel user = null;
            try
            {
                user = Context.Users.FirstOrDefault(x => x.Id.Equals(id));
            }
            catch (Exception e)
            {
                if (ExceptionHandlingManager.HandleException(e, PolicyNames.DASPolicy)) throw;
            }
            return user;
        }

        public void Delete(Guid id)
        {
            if (id.Equals(Guid.Empty))
               throw ExceptionHelper.CreateEmptyGuidException("id");

            try
            {
                var user = Context.Users.FirstOrDefault(x => x.Id.Equals(id));

                if (user == null)
                    throw new EntityNotFoundException("User");


                Context.Users.Remove(user);
                Context.SaveChanges();

                Logger.Write(string.Format("User with id:{0} successfully deleted", user.Id),
                             LogCategory.DAS,
                             (int)EventPriority.Normal);
            }
            catch (Exception e)
            {
                if (ExceptionHandlingManager.HandleException(e, PolicyNames.DASPolicy)) throw;
            }
            
           
        }

        public void Delete(UserModel item)
        {
            if(item == null)
                throw new ArgumentNullException("item");
            try
            {
                Context.Users.Remove(item);
                Context.SaveChanges();
            }
            catch (Exception e)
            {
                if (ExceptionHandlingManager.HandleException(e, PolicyNames.DASPolicy)) throw;
            }
           
        }

        public IEnumerable<UserModel> GetAll()
        {
            return Get(()=>Context.Users);
        }


        public UserModel FindByLogin(string login)
        {
            if(login == null)
                throw new ArgumentNullException("login");

            return Get(() => Context.Users.FirstOrDefault(x => x.UserName.Equals(login)));
        }

        public UserModel FindByEmail(string email)
        {
            if(email == null)
                throw new ArgumentNullException("email");
            
            UserModel result = null;
            try
            {
                result = Context.Users.FirstOrDefault(x => x.Email.Equals(email));
            }
            catch (Exception e)
            {
                if (ExceptionHandlingManager.HandleException(e, PolicyNames.DASPolicy)) throw;
            }

            return result;
        }
    }
}
